<?php
class userController
{
	function login($username = null, $status = null, &$auth = null)
	{
		if($_POST)
		{
			// validate form
			if(PFBC\Form::isValid("login", false))
			{
				$valid = true;
				
				if(!$auth->checkAuth())
				{
					print_r($auth);die;
					
					$valid = false;
					PFBC\Form::setError("login", "De combinatie e-mailadres met wachtwoord is niet gevonden.", "custom_password");
				}
				
				if(!$valid)
				{
					header("Location: " . $_SERVER["REQUEST_URI"]);
				}
				else
				{
					PFBC\Form::clearValues("login");
					PFBC\Form::clearErrors("login");
					
					$smarty = new BlogSmarty();
					$smarty->assign("message", "Succesvol ingelogd.");
					$smarty->assign("url", "/");
					$smarty->display("success.tpl");
				}
			}
			else
			{
				header("Location: " . $_SERVER["REQUEST_URI"]);
			}
		}
		else
		{
			$smarty = new BlogSmarty();
			
			// generate form
			$form = new PFBC\Form("login", "400");
			$form->addElement(new PFBC\Element\Email("E-mailadres", "username", array(
				"validation" => array(
	                new PFBC\Validation\Required,
					new PFBC\Validation\Email
				)
			)));
			
			
			
			$form->addElement(new PFBC\Element\Password("Wachtwoord", "password", array(
				"validation" => array(
	                new PFBC\Validation\Required
	            )
			)));
			
			$form->addElement(new PFBC\Element\Button("Log in"));
			
			$smarty->assign("form", $form->render(true));
			
			$smarty->assign("title", "Inloggen");
			
			$smarty->display("form.tpl");
			
			PFBC\Form::clearValues("login");
			PFBC\Form::clearErrors("login");
		}
	}
	
	function logout()
	{
		Authentication::logout();
		header("Location: /");
	}
	
	function register()
	{
		die("Registration is not enabled at this time.");
		
		if($_POST)
		{
			// validate form
			if(PFBC\Form::isValid("register", false))
			{
				$valid = true;
				
				// check passwords
				if($_POST["password"] != $_POST["password2"])
				{
					$valid = false;
					PFBC\Form::setError("register", "De opgegeven wachtwoorden zijn niet gelijk aan elkaar.", "custom_password");
				}
				
				// check existing user
				$query = MembersQuery::create();
				$user = $query->findOneByUsername($_POST["email"]);
				if($user != null)
				{
					$valid = false;
					PFBC\Form::setError("register", "Er is al een gebruiker met dit e-mailadres geregistreerd.", "custom_email");
				}
				
				if(!$valid)
				{
					header("Location: " . $_SERVER["REQUEST_URI"]);
				}
				else
				{
					PFBC\Form::clearErrors("register");
					PFBC\Form::clearValues("register");
					
					$member = new Members();
					$member->setUsername($_POST["email"]);
					$member->setPassword( md5($_POST["password"] ) );
					$member->save();
					
					$smarty = new BlogSmarty();
					$smarty->assign("message", "Succesvol geregistreerd.");
					$smarty->assign("url", "/login");
					$smarty->display("success.tpl");
				}
			}
			else
			{
				header("Location: " . $_SERVER["REQUEST_URI"]);
			}
		}
		else
		{
			$smarty = new BlogSmarty();
			
			// generate form
			$form = new PFBC\Form("register", "400");
			$form->addElement(new PFBC\Element\Email("E-mailadres", "email", array(
				"validation" => array(
	                new PFBC\Validation\Required,
					new PFBC\Validation\Email
				)
			)));
			
			$form->addElement(new PFBC\Element\Password("Wachtwoord", "password", array(
				"validation" => array(
	                new PFBC\Validation\Required
	            )
			)));
			
			$form->addElement(new PFBC\Element\Password("Herhaal wachtwoord", "password2", array(
				"validation" => array(
	                new PFBC\Validation\Required
	            )
			)));
			
			$form->addElement(new PFBC\Element\Button("Registreer"));
			
			$smarty->assign("form", $form->render(true));
			
			$smarty->assign("title", "Registreren");
			
			$smarty->display("form.tpl");
			
			PFBC\Form::clearErrors("register");
			PFBC\Form::clearValues("register");
		}
	}
}
?>